#include <stdio.h>
#include <stdlib.h>
#include "SeqQueue.h"

//初始化队列
queue_head *Init_Queue(int maxsize)
{
	queue_head *ptr = NULL;
	ptr = malloc(sizeof(*ptr));
	if(ptr == NULL)
		return NULL;
	ptr->data = calloc(maxsize,sizeof(Datatype));
	ptr->front = 0;
	ptr->rear = 0;
	ptr->maxlength = maxsize;
	ptr->number = 0;
	return ptr;
}

//销毁队列
void Destroy_Queue(queue_head **ptr)
{
	if(*ptr == NULL)
		return ;
	if((*ptr)->data)
	{
	free((*ptr)->data);
	(*ptr)->data = NULL;
	}
	free(*ptr);
	*ptr = NULL;
}

//队列清空
void Clear_Queue(queue_head *ptr)
{
	if(ptr)
	{
	ptr->front = 0;
	ptr->number = 0;
	ptr->rear = 0;
	}
}
//判断队列是否为空
int IsEmpty_Queue(queue_head *ptr)
{
	if(ptr == NULL || ptr->number == 0)
		return 1;
	return 0;

}

//队列的长度
int Length_Queue(queue_head *ptr)
{
	if(ptr == NULL)
		return 0;
	return ptr->number;
}

//出队列
int DeQueue(queue_head *ptr,Datatype *head_data)
{
	if(IsEmpty_Queue(ptr) || ptr->number == 0)
		return -1;
	*head_data = ptr->data[ptr->front++];
	if(ptr->front == ptr->maxlength)	//当front等于最大长度时，front从头开始
	{
		ptr->front = 0;
	}
	ptr->number--;
}

//入队列
int EnQueue(queue_head *ptr,Datatype data)
{
	if(ptr == NULL || ptr->data == NULL || ptr->number == ptr->maxlength)
		return -1;
	ptr->data[ptr->rear++] = data;
	if(ptr->rear == ptr->maxlength)
	{
		ptr->rear = 0;
	}
	ptr->number++;
	return 0;
}

//获得队列头元素
int GetHead_Queue(queue_head *ptr,Datatype *head_data)
{
	if(ptr == NULL || ptr->number == 0)
		return -1;
	*head_data = ptr->data[ptr->front];
		return 0;
}
